package com.livesource.client.web.liveTaskBoard;

import java.util.HashMap;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.livesource.client.LiveSourceGlobalVariables;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.utilities.ButtonClose;
import com.livesource.client.utilities.ButtonRefresh;
import com.livesource.client.utilities.LoadingPanel;
import com.livesource.client.web.mainMenu.LiveSourceMenu;
import com.livesource.client.web.sourceCode.FeatureDescription;
import com.livesource.client.web.sourceCode.SourceCodePage;
import com.livesource.client.web.sourceCode.filesTree.FilesTree;
import com.livesource.client.web.sourceCode.filesTree.FilesViewList;
import com.livesource.client.web.sourceCode.viewOptions.OptionsIcons;

/**
 * 
 * @Feature
 */
public class LiveTaskBoard extends VerticalPanel {

	private static VerticalPanel vpNotStarted = new VerticalPanel();
	private static VerticalPanel vpInProgress = new VerticalPanel();
	private static VerticalPanel vpDone = new VerticalPanel();
	private static VerticalPanel vpBlocked = new VerticalPanel();

	public LiveTaskBoard() {

		this.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		this.setSpacing(30);

		HorizontalPanel hpTitle = new HorizontalPanel();
		hpTitle.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		hpTitle.setSpacing(20);

		HTML htmlTitle = new HTML(
				"<b><font color='blue' size='3'>Live Task Board</font></b>");
		hpTitle.add(htmlTitle);

		Label space = new Label(" ");
		space.setWidth("400px");
		hpTitle.add(space);

		hpTitle.add(new ButtonRefresh(buttonRefreshClickHandler()));

		hpTitle.add(new ButtonClose());

		this.add(hpTitle);

		VerticalPanel vpBoard = new VerticalPanel();

		HorizontalPanel hpTitles = new HorizontalPanel();

		Label space2 = new Label(" ");
		space2.setWidth("70px");
		hpTitles.add(space2);

		HTML htmlNotStarted = new HTML("<B>Not Started</B>");
		htmlNotStarted.setWidth("250px");
		hpTitles.add(htmlNotStarted);

		HTML htmlInProgress = new HTML("<B>In Progress</B>");
		htmlInProgress.setWidth("250px");
		hpTitles.add(htmlInProgress);

		HTML htmlDone = new HTML("<B>Done</B>");
		htmlDone.setWidth("250px");
		hpTitles.add(htmlDone);

		HTML htmlBlocked = new HTML("<B>Blocked</B>");
		htmlBlocked.setWidth("250px");
		hpTitles.add(htmlBlocked);

		vpBoard.add(hpTitles);

		HorizontalPanel hpBoard = new HorizontalPanel();

		vpNotStarted.setSpacing(15);
		vpNotStarted.setWidth("250px");
		vpNotStarted.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		vpNotStarted
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		hpBoard.add(vpNotStarted);

		VerticalPanel vpSeparation1 = new VerticalPanel();
		vpSeparation1.setSize("1px", "500px");
		vpSeparation1.setStyleName("taskBoard");
		vpSeparation1.add(new Label(" "));
		hpBoard.add(vpSeparation1);

		vpInProgress.setSpacing(15);
		vpInProgress.setWidth("250px");
		vpInProgress.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		vpInProgress
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		vpInProgress.add(new LoadingPanel());

		hpBoard.add(vpInProgress);

		VerticalPanel vpSeparation2 = new VerticalPanel();
		vpSeparation2.setSize("1px", "500px");
		vpSeparation2.setStyleName("taskBoard");
		vpSeparation2.add(new Label(" "));
		hpBoard.add(vpSeparation2);

		vpDone.setSpacing(15);
		vpDone.setWidth("250px");
		vpDone.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		vpDone.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		hpBoard.add(vpDone);

		VerticalPanel vpSeparation3 = new VerticalPanel();
		vpSeparation3.setSize("1px", "500px");
		vpSeparation3.setStyleName("taskBoard");
		vpSeparation3.add(new Label(" "));
		hpBoard.add(vpSeparation3);

		vpBlocked.setSpacing(15);
		vpBlocked.setWidth("250px");
		vpBlocked.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		vpBlocked.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		hpBoard.add(vpBlocked);

		vpBoard.add(hpBoard);

		this.add(vpBoard);
	}

	public static void showTasks(HashMap<String, ProjectClass> entries) {

		vpNotStarted.clear();
		vpInProgress.clear();
		vpDone.clear();
		vpBlocked.clear();

		for (ProjectClass entry : entries.values()) {

			if (ProjectClass.TODO_STATUS.equals(entry.getStatus())) {

				vpNotStarted.add(getDescription(entry));

			} else if (ProjectClass.INPROGRESS_STATUS.equals(entry.getStatus())) {

				vpInProgress.add(getDescription(entry));

			} else if (ProjectClass.FEATURE_TYPE.equals(entry.getFeatureType())) {

				vpDone.add(getDescription(entry));

			} else if (ProjectClass.BLOCKED_STATUS.equals(entry.getStatus())) {

				vpBlocked.add(getDescription(entry));
			}
		}
	}

	private static VerticalPanel getDescription(final ProjectClass entry) {

		VerticalPanel vp = new VerticalPanel();

		vp.setBorderWidth(1);

		VerticalPanel vpIn = new VerticalPanel();

		vpIn.setSpacing(5);

		String task = entry.getFeatureName();

		if (entry.getFeatureDescription() != null
				&& !entry.getFeatureDescription().equals("")) {

			task += ": " + entry.getFeatureDescription();
		}

		HTML htmlName = new HTML("<font size=1><a href=#>" + task
				+ "</a></font>");

		htmlName.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				LiveSourceMenu.popup.clear();

				LiveSourceMenu.popup.setWidget(new SourceCodePage(
						LiveSourceMenu.MENU_FEATURES));

				FilesTree tree = new FilesTree(FilesViewList.VIEW_FEATURES,
						LiveSourceGlobalVariables.projectEntries);

				SourceCodePage.featuresTreePanel.clear();
				SourceCodePage.featuresTreePanel.add(tree);

				SourceCodePage.hpOptionsIcons.clear();
				SourceCodePage.hpOptionsIcons.add(new OptionsIcons(entry));

				SourceCodePage.featureVisualizationPanel.clear();

				SourceCodePage.featureVisualizationPanel
						.add(new FeatureDescription(entry));
			}
		});

		vpIn.add(htmlName);

		vp.add(vpIn);

		return vp;
	}

	private ClickHandler buttonRefreshClickHandler() {

		return (new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				LiveTaskBoard
						.showTasks(LiveSourceGlobalVariables.projectEntries);
			}
		});

	}

}
